package com.zd.lib.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;


import com.zd.lib.BuildConfig;

import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


public class ChannelUtils {

	private static String channelStr = "META-INF/zb";

	public static String getChannel(Context context) {
		ApplicationInfo appinfo = context.getApplicationInfo();
		String sourceDir = appinfo.sourceDir;
		String ret = "";
		ZipFile zipfile = null;
		try {
			zipfile = new ZipFile(sourceDir);
			Enumeration<?> entries = zipfile.entries();
			while (entries.hasMoreElements()) {
				ZipEntry entry = ((ZipEntry) entries.nextElement());
				String entryName = entry.getName();
				if (entryName.startsWith(channelStr)) {
					ret = entryName;
					break;
				}
			}
		} catch (IOException e) {
			if (BuildConfig.DEBUG) {
				LogUtils.e("channel",e);
			}
		} finally {
			if (zipfile != null) {
				try {
					zipfile.close();
				} catch (IOException e) {
					if (BuildConfig.DEBUG) {
						LogUtils.e("channel",e);
					}
				}
			}
		}
		String[] split = ret.split("_");
		if (split != null && split.length >= 2) {
			return ret.substring(split[0].length() + 1);
		} else {
			return "";
		}
	}
}
